//
//  HomeStatusViewModel.swift
//  BQWB
//
//  Created by laibingqiang on 16/10/26.
//  Copyright © 2016年 laibingqiang. All rights reserved.
//

import UIKit
import SDWebImage
class HomeStatusViewModel: NSObject {
    
    var statusModel:HomeStatusModel?{
        
        
        didSet{
           
            levelImg = getLevelImage(mbrank: (statusModel?.user?.mbrank)!)
            
            verifiedImg = getVerified(verified: (statusModel?.user?.verified)!)
            
            reposts_count = getCountString(count: (statusModel?.reposts_count)!,title: "转发")
            
            comments_count = getCountString(count: (statusModel?.comments_count)!,title: "评论")
            
            attitudes_count = getCountString(count: (statusModel?.attitudes_count)!,title: "赞")
            
            isRetweeted_status = isRetweeted_status(retweeted_status: statusModel?.retweeted_status)
            
            retweeted_status_content = "@\(statusModel?.retweeted_status?.user?.name ?? ""):\(statusModel?.retweeted_status?.text ?? "")"
            
            come_where = getCome_where(source: (statusModel?.source) ?? "")
            
            public_time = getPublic_time(time: statusModel?.created_at)
            
        }
    }
    
    //用户等级
    var levelImg:UIImage?
    //认证等级
    var verifiedImg:UIImage?
    ///int	转发数
    var reposts_count: String?
    /// 评论
    var comments_count: String?
    /// int	表态数
    var attitudes_count: String?
    //判断是否有转发微博
    var isRetweeted_status:Bool?
    //如果有转发微博的内容
    
    var retweeted_status_content:String?
    //微博来自哪里
    var come_where:String?
    //微博发布时间
    var public_time:String?
    
    
}
//对时间的处理
extension HomeStatusViewModel{
//    - 判断时间是否是今年
//    - 判断是否是今天
//    - 判断是否是1分钟之前
//    - 刚刚
//    - 判断是否是1小时之前
//    - xx 分钟前
//    - 其它
//    - xx 小时前
//    
//    
//    - 判断是否是昨天
//    - 昨天 08:10
//    - 其它
//    - 10-30 10:20
//    
//    - 不是今年
//    2015-10-10 10:10
    func getPublic_time(time:Date?)->String{
        let dt = DateFormatter()
        
        dt.locale = Locale(identifier: "en_US")
        //是今年
        if isThisYear(time: time!){
            
            let calendar = Calendar.current
            //是今天
            if calendar.isDateInToday(time!){
                
                let timeInterval = abs(Int((time?.timeIntervalSinceNow)!))
                if timeInterval < 60{
                    return "刚刚"
                }
                else if timeInterval < 3600{
                    
                    let m = Int(timeInterval / 60)
                    return "\(m)分钟前"
                }
                else
                {
                    let h = Int(timeInterval / 3600)
                    return "\(h)小时前"
                }
                
            }
            else if calendar.isDateInYesterday(time!){
            
                dt.dateFormat = "昨天 HH:mm"
            }
            else{
                dt.dateFormat = "MM-dd HH:mm"
            }
            
            
        }
        else
        {
            dt.dateFormat = "yyyy-MM-dd HH:mm"
            
            
        }
        
        return dt.string(from: time!)
    }

    //判断是不是今年的方法
    func isThisYear(time:Date)->Bool{
        
        let dt = DateFormatter()
        //格式化方式
        dt.dateFormat = "yyyy"
        //指定本地化信息
        dt.locale = Locale(identifier: "en_US")
        //当前时间
        let currentYear = dt.string(from: Date())
        //微博发布时间
        let createAtYear = dt.string(from: time)
        
        return currentYear == createAtYear
        
    }
}

extension HomeStatusViewModel{
    
    //获取用户微博来自哪里的方法
    func getCome_where(source:String)->String{
        
        if let startRange = source.range(of: "\">"), let endRange = source.range(of: "</") {
            
            //  获取开始光标位置索引
            //  upperBound表示范围的结束光标位置索引
            let startIndex = startRange.upperBound
            //  获取范围的开始光标位置索引
            let endIndex = endRange.lowerBound
            //  获取指定范围的字符串
            let str =  "来自:" + source.substring(with: startIndex..<endIndex)
            return str
        }
        return "未知的地方"
    }
}

extension HomeStatusViewModel{
    
    //用户等级图片的方法
    func getLevelImage(mbrank:Int)->UIImage?{
        
        if mbrank > 0 && mbrank < 7{
            return UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        
        return UIImage(named: "common_icon_membership_expired")
    }
    
    //认证等级图片的方法
    func getVerified(verified:Int)->UIImage?{
        
        switch verified {
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 200:
            return UIImage(named: "avatar_grassroot")
        default:
            return UIImage(named: "avatar_vgirl")
        }
        
    }
    
//    - 如果 count <= 0 显示 转发 评论 赞
//    - 如果 count > 0 && count < 10000  是多少显示多少 例如 7890 显示就是 7890
//    - 如果 count >= 10000  显示 x.x 万    例如12000  显示 就是1.2万
//    - 例如 20000  显示 2万   30000 显示 3万
    //转发、评论、赞count的显示
    func getCountString(count:Int,title:String)->String?{
        
        if count <= 0{
            return title
        }
        else if count < 10000{
            
            return "\(count)"
        }
        else
        {
            if CGFloat(count/1000)/10 - CGFloat(count/10000) > 0{
                
                return "\(CGFloat(count/1000)/10)万"
            }
            
            return "\(count/10000)万"
        }
    }
    //是否有转发微博的方法
    func isRetweeted_status(retweeted_status:HomeStatusModel?)->Bool{
        
        if retweeted_status == nil{
            
            return false
        }
        
        return true
    }
}
